import asunit.framework.TestCase;

import org.molamil.acto.commands.CommandsParser;
import org.molamil.acto.commands.CommandContext;
import org.molamil.acto.core.ParserError;

/**
 * @author Jorge Hernandez
 */
class org.molamil.acto.commands.CommandsParserTest extends TestCase {
	
	
	private var className:String = "org.molamil.acto.props.CommandsParserTest";
	private var instance:CommandsParser;

	
	public function setUp():Void {
		instance = new CommandsParser();
	}

	public function tearDown():Void {
		delete instance;
	}

	public function testInstantiated():Void {
		assertTrue("CommandsParser instantiated", instance instanceof CommandsParser);
	}
	
	public function testParse():Void {
		
		//TODO: Test depends.		//TODO: Test params mode.
		
		var s:String = '' +
			'<commands>' +
    			'<command name="login" title="Login functionality" class="foo.LoginCmd"' + 
    				'target="http://www.molamil.org/Login" role="">' +
    				'<prop name="defaultUsername">Jorge</prop>' +    				'<prop name="defaultPassword">mola</prop>' +
    				'<param name="username" required="true" validator="dk.wunderman.cokecom2008.vld.RegexVld">' +
						'<prop name="pattern" value="^([0-9a-zA-Z]{5,20})$" />' +
						'<prop name="msgInvalid"><![CDATA[Invalid username.]]></prop>' +
					'</param>' +
					'<param name="password" required="true" validator="dk.wunderman.cokecom2008.vld.RegexVld">' +
						'<prop name="pattern" value="^([0-9a-zA-Z]{5,20})$" />' +
						'<prop name="msgInvalid"><![CDATA[Invalid password.]]></prop>' +
					'</param>' +
    			'</command>' +
    			'<command name="participate" title="" class="foo.ParticipateCmd"' + 
    				'target="http://www.molamil.org/Participate" role="user" depends="login">' +
    				'<prop name="action">participate</prop>' +
    			'</command>' +
    			'<command name="participateTwice" class="foo.ParticipateCmd"' + 
    				'target="http://www.molamil.org/Participate" role="user" depends="login, participate" />' +
			'</commands>';
		
		var xml:XML = new XML(s);
		var a:Array = CommandsParser.parse(xml);
		
		assertTrue("3 commands parsed", a.length == 3);		assertTrue("a[0] is a CommandContext", a[0] instanceof CommandContext);
		
		var loginCmd:CommandContext = a[0];
		
		assertTrue("login cmd, name", loginCmd.name == "login");		assertTrue("login cmd, title", loginCmd.title == "Login functionality");
		assertTrue("login cmd, class", loginCmd.className == "foo.LoginCmd");		assertTrue("login cmd, target", loginCmd.target == "http://www.molamil.org/Login");		assertTrue("login cmd, role", loginCmd.role == null);		assertTrue("login cmd, depends", loginCmd.depends == null);		assertTrue("login cmd, props.defaultUsername", loginCmd.props.defaultUsername == "Jorge");		assertTrue("login cmd, props.defaultPassword", loginCmd.props.defaultPassword == "mola");
		
		var participateCmd:CommandContext = a[1];
		
		assertTrue("participate cmd, name", participateCmd.name == "participate");
		assertTrue("participate cmd, title", participateCmd.title == "");		assertTrue("participate cmd, title not null", participateCmd.title != null);
		assertTrue("participate cmd, class", participateCmd.className == "foo.ParticipateCmd");
		assertTrue("participate cmd, target", participateCmd.target == "http://www.molamil.org/Participate");
		assertTrue("participate cmd, role", participateCmd.role == "user");
		assertTrue("participate cmd, depends", participateCmd.depends[0] == "login");		assertTrue("participate cmd, depends length", participateCmd.depends.length == 1);
		assertTrue("participate cmd, props.action", participateCmd.props.action == "participate");
		
		var participateTwiceCmd:CommandContext = a[2];
		
		assertTrue("participateTwice cmd, name", participateTwiceCmd.name == "participateTwice");
		assertTrue("participateTwice cmd, title", participateTwiceCmd.title == null);
		assertTrue("participateTwice cmd, class", participateTwiceCmd.className == "foo.ParticipateCmd");
		assertTrue("participateTwice cmd, target", participateTwiceCmd.target == "http://www.molamil.org/Participate");
		assertTrue("participateTwice cmd, role", participateTwiceCmd.role == "user");
		assertTrue("participateTwice cmd, depends", participateTwiceCmd.depends[0] == "login");		assertTrue("participateTwice cmd, depends", participateTwiceCmd.depends[1] == "participate");
		assertTrue("participateTwice cmd, depends length", participateTwiceCmd.depends.length == 2);
		
		try {
			CommandsParser.parse(new XML("<commandsTest/>"));
			fail("Bad root node didn't throw an error");
		} catch (e:ParserError) {}

	}
	
	
}
